package com.fili.gui;

import sun.awt.VerticalBagLayout;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;

import com.fili.core.BatchJob;
import com.fili.core.ExecThread;
import com.fili.core.CompileParam;
import com.fili.gui.components.BatchPanel;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Oct 5, 2006
 * Time: 12:38:34 PM
 * Revision: 0
 */
public class MainFrame extends JFrame
{
    private JProgressBar progressBar;
    private JLabel lblEstimated;
    private JButton btnStartStop;
    private boolean started;
    private BatchJob batchJob;
    private BatchPanel batchPanel;
    private int nrOp;

    private ExecThread execThread;
    private long startTime;

    private JFrame aux;
    private JTextArea textArea;

    private FileOutputStream fos;


    public MainFrame(BatchJob batchJob)
    {
        this.batchJob = batchJob;

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(750, 400);

        getContentPane().setLayout(new BorderLayout());

        batchPanel = new BatchPanel(batchJob);
        getContentPane().add(new JScrollPane(batchPanel), BorderLayout.CENTER);

        JPanel bottomPnl = new JPanel();
        bottomPnl.setLayout(new BoxLayout(bottomPnl, BoxLayout.Y_AXIS));
        getContentPane().add(bottomPnl, BorderLayout.SOUTH);

        progressBar = new JProgressBar();
        progressBar.setValue(0);
        bottomPnl.add(progressBar);

        lblEstimated = new JLabel(" ");
        bottomPnl.add(lblEstimated);

        JPanel pnl = new JPanel(new BorderLayout());
        btnStartStop = new JButton("START");
        btnStartStop.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                execStart();
            }
        });
        pnl.add(btnStartStop, BorderLayout.EAST);
        bottomPnl.add(pnl);

        setVisible(true);
    }

    private String convertDigit(long v)
    {
        if (v < 10)
            return "0" + v;
        return "" + v;
    }

    private String convertTime(long v)
    {
        v /= 1000;

        long h = v / 3600;
        v = v % 3600;
        long m = v / 60;
        long s = v % 60;


        return convertDigit(h) + ":" + convertDigit(m) + ":" + convertDigit(s);
    }

    public void updateProgress(int v)
    {
        if (v < 0)
        {
            execStart();
            progressBar.setValue(0);
            lblEstimated.setText("Finished in " + convertTime(System.currentTimeMillis() - startTime));
        }
        else
            progressBar.setValue(v);

        if (v == 0)
        {
            startTime = System.currentTimeMillis();
            lblEstimated.setText("Unknown time");
        }
        else
        {
            long time = System.currentTimeMillis();
            long dif = time - startTime;
            lblEstimated.setText("Elapsed: " + convertTime(dif) + "   Remaining: " + convertTime((dif * progressBar.getMaximum()) / v));
        }
    }

    public synchronized void addOutputText(String s, boolean error)
    {
        textArea.append(s + "\n");
//        textArea.scrollRectToVisible(new Rectangle(0, textArea.getPreferredSize().height - 5, 5, 5));
        if (fos != null)
            try
            {
                fos.write((s+"\n").getBytes());
                fos.flush();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
    }

    private void execStart()
    {
        if (started)
        {
            started = false;
            btnStartStop.setText("START");
            execThread.stop();
        }
        else
        {
            started = true;
            btnStartStop.setText("STOP");

            try
            {
                fos = new FileOutputStream("lastbuild.log");
            }
            catch (FileNotFoundException e)
            {
                e.printStackTrace();
                fos = null;
            }

            batchJob.selections = new ArrayList();

            ArrayList al = batchJob.selections;

            nrOp = 1;
            for (int i = 0; i < batchPanel.paramGroup.length; i++)
            {
                ArrayList list = batchPanel.paramGroup[i].getSelection();
                if (list.size() > 0)
                {
                    al.add(((CompileParam) (batchJob.params.get(i))).getName());
                    al.addAll(list);
                    nrOp *= list.size();
                    al.add(0, al = new ArrayList());
                }
            }

            progressBar.setMaximum(nrOp);

            aux = new JFrame("Output");
            aux.setSize(400, 700);
            textArea = new JTextArea();
            aux.getContentPane().add(new JScrollPane(textArea));
            aux.setVisible(true);

            execThread = new ExecThread(batchJob, this);
            execThread.start();
        }
    }
}
